package org.xtense.rcp.util;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.xtense.rcp.Activator;

public class ImagesContext {
	private static ImageRegistry imageRegistry;
	
	public final static String Element = "element";
	public final static String Attribute = "attribute";
	public final static String Text = "text";
	
	public final static String Open = "open";
	public final static String Save = "save";
	public final static String Store = "store";
	public final static String Refresh24 = "refresh24";
	public final static String Update = "update";
	
	public final static String Refresh = "refresh";
	
	public final static String OpenDB = "opendb";
	public final static String Delete = "delete";
	
	public static ImageRegistry getImageRegistry() {
		if(imageRegistry == null) {
			imageRegistry = new ImageRegistry();
			declareImages();
		}
		return imageRegistry;
	}
	
	private final static void declareImages() {
		declareRegistryImages(Element, "icons/element.gif");
		declareRegistryImages(Attribute, "icons/attribute.gif");
		declareRegistryImages(Text, "icons/text.gif");
		declareRegistryImages(Open, "icons/open.png");
		declareRegistryImages(Save, "icons/save.png");
		declareRegistryImages(Store, "icons/store.png");
		declareRegistryImages(Update, "icons/update.png");
		declareRegistryImages(Refresh, "icons/refresh.png");
		declareRegistryImages(OpenDB, "icons/opendb.png");
		declareRegistryImages(Delete, "icons/delete.png");
		declareRegistryImages(Refresh24, "icons/refresh24.png");
	}
	
	private final static void declareRegistryImages(String key,String path) {
		try {
			URL url = FileLocator.find(Activator.getDefault().getBundle(), new Path(path), null);
			if(url != null) {
				ImageDescriptor image = ImageDescriptor.createFromURL(url);
				getImageRegistry().put(key, image);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Image getImage(String key) {
		return getImageRegistry().get(key);
	}
	
	public static ImageDescriptor getImageDescriptor(String key) {
		return getImageRegistry().getDescriptor(key);
	}
	
}
